MySQL LIKE Operator

The LIKE operator is used to filter the records based on the any pattern search.
There are 2 conjuntions used in LIKE operator. They are % and _.
The % represent the pattern with zero or multiple characters.
The _ represents a single character matching pattern.

Syntax for LIKE Operator

SELECT column_1, column_2, ...
FROM table_name
WHERE column_N LIKE pattern;

We can use the LIKE operator in following pattern.
'xx%' - Start's with xx and ends with any no.of characters.
'%x' - Starts with any no.of characters and end's with x.
'%xx%' - Finds any value that contains xx pattern in it.
'x%' - Find the value that have x in the second position.
'x
%' - Finds any values that start with x and are at least 2 characters in length.
'a%z' - Finds any values that start with a and ends with z.

Consider the following employee table with the defult entries.

empno name age role location salary
001 Andrew 30 Manager India 100000
002 Beslin 28 Business Analyst India 50000
003 Joanna 23 Senior Developer USA 500000
004 Rayan 26 Technical Lead Canada 500000

Example 1

select * from employee where name like 'an%';

Output

empno name age role location salary
001 Andrew 30 Manager India 100000

Example 2

select * from employee where name like '%na';

Output

empno name age role location salary
003 Joanna 23 Senior Developer USA 500000

Example 3

select * from employee where name like '%ya%';

Output

empno name age role location salary
004 Rayan 26 Technical Lead Canada 500000

Example 4

 select * from employee where name like '%__n';

Output

empno name age role location salary
002 Beslin 28 Business Analyst India 50000
004 Rayan 26 Technical Lead Canada 500000

Example 5

select * from employee where name like 'R%n';

Output

empno name age role location salary
004 Rayan 26 Technical Lead Canada 500000

Most Read